package io.renren.modules.onlinestudy.service.impl;

import io.renren.common.config.FileServerConfig;
import io.renren.common.exception.RRException;
import io.renren.common.utils.DfsUtil;
import io.renren.common.utils.FileServerResponse;
import io.renren.modules.onlinestudy.dao.XcBmDao;
import io.renren.modules.onlinestudy.entity.*;
import io.renren.modules.onlinestudy.service.XcBmService;
import io.renren.modules.sys.entity.SysDictEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;



@Service("xcBmService")
public class XcBmServiceImpl implements XcBmService {
	@Autowired
	private XcBmDao xcBmDao;

	@Autowired
	private FileServerConfig fileServerConfig;
	
	@Override
	public XcBmEntity queryObject(Integer id){
		return xcBmDao.queryObject(id);
	}
	
	@Override
	public List<XcBmEntity> queryList(Map<String, Object> map){
		return xcBmDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return xcBmDao.queryTotal(map);
	}
	
	@Override
	public void save(XcBmEntity xcBm){
		xcBmDao.save(xcBm);
	}
	
	@Override
	public void update(XcBmEntity xcBm){
		xcBmDao.update(xcBm);
	}
	
	@Override
	public void delete(Integer id){
		xcBmDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		xcBmDao.deleteBatch(ids);
	}

    @Override
    public void executeSh(XcBmEntity xcBmEntity) {
        xcBmDao.update(xcBmEntity);
    }

    public String queryXyxxAvatar(XcBmEntity xcBmEntity){
       return xcBmDao.queryXyxxAvatar(xcBmEntity);
    }

    public List<String> queryXyxxImgDate(XcBmEntity xcBmEntity) {
        return xcBmDao.queryXyxxImgDate(xcBmEntity);
    }

    public List<XyxxImage> queryXyxxImgs(XyxxImage xyxxImage){
        return xcBmDao.queryXyxxImgs(xyxxImage);
    }

    public void updateImgarr(XyxxSh xyxxSh) {
        xcBmDao.updateImgarr(xyxxSh);
    }

    public List<XyxxImage> selImgArrs(XcBmEntity xcBmEntity){
        return xcBmDao.selImgArrs(xcBmEntity);
    }

    public List<SysDictEntity> getStudyAddresss(){
        return xcBmDao.getStudyAddresss();
    }

    public List<StudyStatistics> getGroupStudyStatistics(Map<String, Object> map) {
        return xcBmDao.getGroupStudyStatistics(map);
    }

    public List<StudyStatistics> getXxtj(){
        return xcBmDao.getXxtj();
    }

    @Override
    public List<XcVideoEntity> xxVideoList(Map<String, Object> map){
        return xcBmDao.xxVideoList(map);
    }

    @Override
    public int queryxxVideoTotal(Map<String, Object> map) {
        return xcBmDao.queryXxVideoListTotal(map);
    }

    @Override
    public void deleteVideosBatch(Integer[] ids) {
        xcBmDao.deleteVideosBatch(ids);
    }

    @Override
    public void xxVideoSave(XcVideoEntity xcVideo) throws IOException {
        MultipartFile videoFile = xcVideo.getVideoFile();
        MultipartFile imageFile = xcVideo.getImageFile();
        FileServerResponse video_response = DfsUtil.uploadFileByMultipartFile(videoFile, fileServerConfig.getFileUploadUrl());
        FileServerResponse image_response = DfsUtil.uploadFileByMultipartFile(imageFile, fileServerConfig.getImageUploadUrl());
        if(!video_response.isSuccess()){
            throw new RRException(video_response.getMessage());
        }
        if (!image_response.isSuccess()) {
            throw new RRException(image_response.getMessage());
        }
        xcVideo.setVideo(video_response.getHttpUrl());
        xcVideo.setImg(image_response.getHttpUrl());
        xcVideo.setLdate(new Date());
        xcBmDao.xxVideoSave(xcVideo);
    }
}
